package com.zhijiaoyun.alipay;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;

/**
 * @author shisfish
 * @date 2017/6/6
 */
public class AlipayConfig {
	public static String SERVICE_URL = "https://openapi.alipay.com/gateway.do";
	
	public static String APP_ID = "2017061107467648";
	
	
	/**
	 * 商户的私钥,需要PKCS8格式，RSA公私钥生成：https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.nBDxfy&treeId=58&articleId=103242&docType=1
	 */
	public static String PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDpAlMnf7jBZAfRfwo9dVc6e1/kz9CZLxd4bYNEq8p3IlXgbQLRsfXPAz/QRRsgrtrFhAQxhPae/+juotmCB6mddUsNzrzOr1YlAONSdRYnBpKKuGbHwSkoMC3lj95vrdUtoNSfCs1evP1oNxjPM2ihSELVztYKXYy/Cxgt9/VQcYGpUTroH6xOHRk9nP7p3Z/FV02u63HN8jSm6eAckMjEdsAqKf5A8//85Wtuvc3y30/hzjzHFGEl4xyK8RAUbn/w9PiuqMsxBPMjvP1tL+eEs2itq+Xk/P3vSmHwfY7NPS+9FEpf4F3JjRHiBYpKx3oGEPwNoXcNhJPYBeUvqZEbAgMBAAECggEAd+8HaCxYp8paL94JIks0o9h/KmJBR6eyl/0edQlLxHh65XKW4eKM2IoenZAEf5Ug7EugcPC0lHC6r6JI/KH/DxohkLDaBDTBzXggif/UcC/028xAsw7cpV/vL9sWrjs2oC7JYjeIL/xtHuhFL9mRQHrWVAELsPgoCuml/3tC3K0kmAkQTlgPPxWIOvnfFL/qnhKkq7ZKD2V8x7fLtmJtrbpvUglH2lbslu1N2C1koPHVlDAluvv9S2g5yYhVOYtJMTKNb6fEUWauxYw+r2h0GR7Xqdx3WeKlC9MWQ883koKFf7ufDl/dSCAfFpDoSa3pXfubGxLnx1anOC2Gbcy4GQKBgQD0SVdcQrW0O/fgv+ybELMAkRwzgpZH1gwBT2ZY4+SonfpeCRByBjrIAPdr5etRHhBlxn2IGZc7IOIxFQuKZyXM1qi6ztkdDfKdzozL/RfFxe1ImAYO4gy4TSHYCqH8xxvAtqFNlER4tOBhqJh6DO7pnnJOS5RsFC9mf6kUedsyZQKBgQD0Lo0rdYBzZhTdLRm/b3Gsy8gINRFjY6DtP4sXV8Vjj/MPUnJRNmomPArcZMmtojV19O6gRRTX07WDj67gJXmNVBzhqJ1nO0u0dZAWB2z1OUaxPeQulgGHqW14DKBT5RJ5xuH6c0vZPfK31qXFBcELnuX8AMv50DhEMHoHQeu9fwKBgQDQm3TDr5/1SqSRw7Nsc+U4XbKkdiaZL7tOvxwaWnogV8++z6TAintV4iVneY7q1W6RSuBWAex4LMubuGoDhbPs6X5rEEqtmgSOKB1p8TaEyFGqBVBfE0BjflKhZ+sOroUPm6XBVxTlsyhNnSigST0YORtJqxcS+GXF9MxRVQIPVQKBgGGj07NTEwASC7m9dRRFnfD4s44EmmVhy1HTHO1CnZ2ITCsOTsQGvD95tuYWL0cNG8+FF35i4UjEUUrAQ69BjO9keQALCpXi8gzBDdKhOYJ9WrU5CGtQdGAo8asVcW7apJ6pVmdtfyDpUosMe9Di0iSxb0lrkCTejAFIWSNZyshFAoGBAOc1dsgpEESfpMNUONukaO12h6nYEXMlikdcwazggJq4X49uEaq22JAtPAPIN8R8dayPdVqbJp2sX3eYzIIyJL2PYE+/xEE1sbKbfKWnPFf4gYKsNyHQlyrtLRmE4//0j7rM/SdnkFD5uKUIEj2Bc3eDYRT7CsV3EEQoWns0shBF";
	public static String FORMAT = "json";
	/**
	 * 字符编码格式 目前支持 gbk 或 utf-8
	 */
	public static String CHARSET = "utf-8";
	/**
	 * 支付宝的公钥，查看地址：https://openhome.alipay.com/platform/keyManage.htm?keyType=partner
	 */
	public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvXymAnrQVF+AKoMBcMHADj1GHzlrMmMJzdXC2VnVSxxxu2RPCeiyr+xmneJRznYpeWWotW8KeZQLiV+SWPBeN4jcKRB9eQxMQFJIPbXYfOpG6RHPJRk6T8JmAEhpR9wg1sVB0Qemn1bubif0mSljujzxI7E3TIfXJ8wM8xiuYUwWbZjQTYfRHkyDxBmvDNZ7A4meCIrbSa61XNm7lpTw0cgyIxYZ26h/eaVo/OvoR49qV0KXdX/+25O7YjnfYLJT+SWgZ65MUV5Ld9rCvxW0QUPpFg29JheW6LXZVsEC18Ir0ofEs/VkA9REpSi7M3sHZp4t40I9IHenH3Ats7NjJwIDAQAB";
	/**
	 * 签名方式
	 */
	public static String SIGN_TYPE = "RSA2";
	public static String AES_KEY = "8EBAZjvnVRB3nbH9D9WElQ==";
	public static AlipayClient alipayClient;
	static {
		alipayClient = new DefaultAlipayClient(SERVICE_URL, APP_ID, PRIVATE_KEY, FORMAT,
				CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE, AES_KEY, "AES");
    }
	
	public static String RETURN_URL = "http://123.206.192.21:8080/zhijiaoyun-java/api/alipay/returnUrl";
	public static String NOTIFY_URL = "http://123.206.192.21:8080/zhijiaoyun-java/api/alipay/notifyUrl";

//	public static String RETURN_URL = "http://121.196.206.205/zhijiaoyun-java/api/alipay/returnUrl";
//	public static String NOTIFY_URL = "http://121.196.206.205/zhijiaoyun-java/api/alipay/notifyUrl";

	/**
	 * 合作身份者ID，签约账号，以2088开头由16位纯数字组成的字符串，查看地址：https://openhome.alipay.com/platform/keyManage.htm?keyType=partner
	 */
	public static String partner = "2088621914430927";


	/**
	 * 调试用，创建TXT日志文件夹路径，见AlipayCore.java类中的logResult(String sWord)打印方法。
	 */
	public static String log_path ="D://";


	/**
	 * 自己的唯一订单号，最长64 非空
	 */
	public static String out_trade_no = "out_trade_no";
	/**
	 * 商品名称，订单标题，最长128 非空
	 */
	public static String subject = "subject";
	/**
	 * 支付类型，默认值1 商品购买 ，最长4 非空
	 */
	public static String payment_type = "1";
	/**
	 * 你的收钱账号 非空
	 */
//	public static String seller_id="158066572@qq.com";
	public static String seller_id="bduymu8159@sandbox.com";
	/**
	 * 支付金额 非空
	 */
	public static BigDecimal total_fee = new BigDecimal("0.01");
	/**
	 * 具体商品的描述 最长512 非空
	 */
	public static String body = "body";
	/**
	 * 交易类型 1：实物交易（默认）；0：虚拟交易。 可空
	 */
	public static String goods_type = "1";
	/**
	 *  花呗分期参数 可空
	 *  Json格式。 hb_fq_num：花呗分期数，比如分3期支付； 
	 *  hb_fq_seller_percent：卖家承担收费比例，比如100代表卖家承担100%。
	 * 两个参数必须一起传入。
	 * 具体花呗分期期数和卖家承担收费比例可传入的数值请咨询支付宝。
	*/
	public static String hb_fq_param = "{“hb_fq_num”:“3”,“hb_fq_seller_percent”:“100”}";
	/**
	 * 是否发起实名检验 1  TF
	 */
	public static String rn_check = "F";
	/**
	 *  未付款交易的超时时间  可空
	 *  取值范围：1m～15d，或者使用绝对时间（示例格式：2014-06-13 16:00:00）
	 *  m-分钟，h-小时，d-天，1c-当天（1c-当天的情况下，无论交易何时创建，都在0点关闭）。
	 *  该参数数值不接受小数点，如1.5h，可转换为90m。
	 */
	public static String it_b_pay = "30m"; 
	/**
	 * 可空 
	 * 开放平台返回的包含账户信息的token（授权令牌，商户在一定时间内对支付宝某些服务的访问权限）
	 * 通过授权登录后获取的alipay_open_id，作为该参数的value，登录授权账户即会为支付账户。
	 */
	public static String extern_token = "extern_token";
	
	public static String setRequestBody(String out_trade_no, String subject, String payment_type, 
			BigDecimal total_fee, String body, String goods_type, String it_b_pay) {
		String string = "{\"out_trade_no\": \"" +
			out_trade_no + "\", \"subject\": \""+
			subject + "\", \"payment_type\": \""+
			payment_type + "\", \"total_fee\": "+
			total_fee + ", \"body\": \""+
			body + "\", \"goods_type\": \""+
			goods_type + "\", \"it_b_pay\": \""+
			it_b_pay + "\"}";
		return string;
	}
	
	public static Map<String, String> toMap(HttpServletRequest request) {
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = iter.next();
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用。
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        return params;
    }
	
}
